package com.onenine.chat.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.onenine.app.model.FriendAddMessage;
import com.onenine.app.utils.DataType;
import com.onenine.chat.commons.IMessage;
import com.onenine.chat.commons.IUser;
import com.onenine.chat.rongcloud.RcUser;
import com.onenine.chat.util.RcMessageConverter;

import io.rong.calllib.message.CallSTerminateMessage;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;
import io.rong.message.GroupNotificationMessage;
import io.rong.message.ImageMessage;
import io.rong.message.InformationNotificationMessage;
import io.rong.message.TextMessage;
import io.rong.message.VoiceMessage;

/**
 * 本地消息
 */
public class LMessage implements IMessage {

    private int msgId;
    private String text;
    private MessageType messageType;
    private IUser user;
    private String sentUserId;
    private String mediaFilePath;
    private long duration;
    private long mDatetime;
    private Message.SentStatus mMsgSendStatus;
    private Message.ReceivedStatus mMsgReceiveStatus;
    private Conversation.ConversationType mConvType;
    private RcExtra mRcExtra;

    public LMessage(String text, Conversation.ConversationType convType, MessageType type) {
        this.text = text;
        this.mConvType = convType;
        this.messageType = type;
        this.mMsgSendStatus = Message.SentStatus.SENT;
        this.mMsgReceiveStatus = new Message.ReceivedStatus(0);
        this.sentUserId = RcUser.getInstance().getUserId();
        this.mDatetime = System.currentTimeMillis();
    }

    public LMessage(Message message) {
        this.mMsgSendStatus = message.getSentStatus();
        this.mMsgReceiveStatus = message.getReceivedStatus();
        this.msgId = message.getMessageId();
        this.sentUserId = message.getSenderUserId();
        this.mConvType = message.getConversationType();
        this.mDatetime = message.getReceivedTime();
        MessageContent messageContent = message.getContent();
        if (messageContent != null) {
            UserInfo userInfo = messageContent.getUserInfo();
            if (userInfo != null) {
                this.user = new LUser(userInfo.getUserId()
                        , userInfo.getName(), userInfo.getPortraitUri().toString());
            }
        }
        if (messageContent instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) messageContent;
            this.text = textMessage.getContent();
            if (message.getMessageDirection() == Message.MessageDirection.SEND) {
                messageType = MessageType.SEND_TEXT;
            } else {
                messageType = MessageType.RECEIVE_TEXT;
            }
        } else if (messageContent instanceof ImageMessage) {
            ImageMessage imageMessage = (ImageMessage) messageContent;
            if (imageMessage.getExtra() != null) {
                try {
                    mRcExtra = JSON.parseObject(imageMessage.getExtra(), ReadBurnExtra.class);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            if (imageMessage.getThumUri() != null) {
                //缩略图
                this.text = imageMessage.getThumUri().toString();
            }
            if (imageMessage.getRemoteUri() != null) {
                //原图
                this.mediaFilePath = imageMessage.getRemoteUri().toString();
            }
            if (message.getMessageDirection() == Message.MessageDirection.SEND) {
                messageType = MessageType.SEND_IMAGE;
            } else {
                messageType = MessageType.RECEIVE_IMAGE;
            }
        } else if (messageContent instanceof VoiceMessage) {
            VoiceMessage voiceMessage = (VoiceMessage) messageContent;
            this.duration = voiceMessage.getDuration();
            this.mediaFilePath = voiceMessage.getUri().getPath();
            if (message.getMessageDirection() == Message.MessageDirection.SEND) {
                messageType = MessageType.SEND_VOICE;
            } else {
                messageType = MessageType.RECEIVE_VOICE;
            }
        } else if (messageContent instanceof FriendAddMessage) {
            FriendAddMessage friendAddMessage = (FriendAddMessage) messageContent;
            text = friendAddMessage.getContent();
            switch (friendAddMessage.getStatus()) {
                case DataType.FRIEND_APPLY:
                    messageType = MessageType.FRIEND_APPLY;
                    break;
                default:
                    messageType = MessageType.EVENT;
                    break;
            }
        } else if (messageContent instanceof CallSTerminateMessage) {
            CallSTerminateMessage callSTerminateMessage = (CallSTerminateMessage) messageContent;
            this.text = RcMessageConverter.convertCallTerminalDesc(callSTerminateMessage);
            if (message.getMessageDirection() == Message.MessageDirection.SEND) {
                messageType = MessageType.SEND_TEXT;
            } else {
                messageType = MessageType.RECEIVE_TEXT;
            }
        } else if (messageContent instanceof GroupNotificationMessage) {
            messageType = MessageType.EVENT;
            text = ((GroupNotificationMessage) messageContent).getMessage();
        } else if (messageContent instanceof InformationNotificationMessage) {
            messageType = MessageType.EVENT;
            text = ((InformationNotificationMessage) messageContent).getMessage();
        } else {
            messageType = MessageType.EVENT;
        }
    }

    @Override
    public int getMsgId() {
        return msgId;
    }

    @Override
    public IUser getFromUser() {
        return user;
    }

    @Override
    public String getSentUserId() {
        return sentUserId;
    }

    @Override
    public Conversation.ConversationType getConversationType() {
        return mConvType;
    }

    public void setFromUser(IUser user) {
        this.user = user;
    }

    public void setMediaFilePath(String path) {
        this.mediaFilePath = path;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    public void setMessageType(MessageType messageType) {
        this.messageType = messageType;
    }

    public void setMessageStatus(Message.SentStatus messageStatus) {
        this.mMsgSendStatus = messageStatus;
    }

    public void setMsgReceiveStatus(Message.ReceivedStatus mMsgReceiveStatus) {
        this.mMsgReceiveStatus = mMsgReceiveStatus;
    }

    public Message.ReceivedStatus getMsgReceiveStatus() {
        return mMsgReceiveStatus;
    }

    @Override
    public String getText() {
        return text == null ? "" : text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public MessageType getMessageType() {
        return messageType;
    }

    @Override
    public Message.SentStatus getMsgSentStatus() {
        return mMsgSendStatus;
    }

    public IUser getUser() {
        return user;
    }

    @Override
    public String getMediaFilePath() {
        return mediaFilePath == null ? "" : mediaFilePath;
    }

    @Override
    public long getDuration() {
        return duration;
    }

    @Override
    public RcExtra getExtra() {
        return mRcExtra;
    }

    @Override
    public void setExtra(RcExtra extra) {
        mRcExtra = extra;
    }

    @Override
    public long getDatetime() {
        return mDatetime;
    }

    public void setDatetime(long datetime) {
        this.mDatetime = datetime;
    }

}